package pers.lolicer.redtidelibrary.repository.entity

import android.content.Context
import androidx.core.net.toUri
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import pers.lolicer.redtidelibrary.repository.MyDatabase
import pers.lolicer.redtidelibrary.utils.FileUtils

@Entity(tableName = "book")
data class BookEntity(
    @PrimaryKey
    @ColumnInfo(name = "uri")
    var uriString: String,

     @ColumnInfo(name = "book_name")
     var book_name:String,

    @ColumnInfo(name = "folder")
    var folder: String,

    @ColumnInfo(name = "time_last_open")
    var time_last_open: Long,

    @ColumnInfo(name = "time_import")
    var time_import: Long
)

fun BookEntity.removeIfNotExist(context: Context){
    val entity = this
    if (!FileUtils().checkFileExistence(context, entity.uriString.toUri())){
        val bookDao = MyDatabase.getDB(context).getBookDao()
        CoroutineScope(Dispatchers.IO).launch{
            bookDao.deleteBookByUri(entity.uriString)
        }
    }
}